using System.Collections;
using System.Collections.Generic;
using UnityEngine;

enum GameState
{
    Ready = 0, Playing = 1, GameOver = 2
};

public class MainController : MonoBehaviour
{
    private RoundController roundController; 
    private View view; 
    private int N = 2; 
    private int gameState;

    void Start()
    {
        Director.GetInstance().mainController = this;
        roundController = gameObject.AddComponent<RoundController>();
        view = gameObject.AddComponent<View>();
        gameState = (int)GameState.Ready;
    }

    public int GetN()
    {
        return N;
    }

    public void Restart()
    {
        view.Init();
        roundController.Reset();
    }

    public void SetGameState(int state)
    {
        gameState = state;
    }

    public int GetGameState()
    {
        return gameState;
    }

    public void ShowPage()
    {
        switch (gameState)
        {
            case 0:
                view.ShowHomePage();
                break;
            case 1:
                view.ShowGamePage();
                break;
            case 2:
                view.ShowRestart();
                break;
        }
    }

    public void SetRoundSum(int roundSum)
    {
        roundController.SetRoundSum(roundSum);
    }

    public void SetViewTip(string tip)
    {
        view.SetTip(tip);
    }

    public void SetViewScore(int score)
    {
        view.SetScore(score);
    }

    public void SetViewRoundNum(int round)
    {
        view.SetRoundNum(round);
    }

    public void SetViewTrialNum(int trial)
    {
        view.SetTrialNum(trial);
    }

    public void Hit(Vector3 position)
    {
        Camera camera = Camera.main;
        Ray ray = camera.ScreenPointToRay(position);

        RaycastHit[] hits;
        hits = Physics.RaycastAll(ray);

        for (int i = 0; i < hits.Length; i++)
        {
            RaycastHit hit = hits[i];
            if (hit.collider.gameObject.GetComponent<DiskData>() != null)
            {
                hit.collider.gameObject.transform.position = new Vector3(0, -6, 0);
                roundController.Record(hit.collider.gameObject.GetComponent<DiskData>());
                view.SetScore(roundController.scoreRecorder.score);
            }
        }
    }

    public void FreeAllFactoryDisk()
    {
        roundController.FreeAllFactoryDisk();
    }
}